package com.glorysoft.xztpda.ui.trackin

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.view.View
import com.glorysoft.xztpda.entity.ui.ToastMessageBean
import com.glorysoft.xztpda.BR
import com.glorysoft.xztpda.R
import com.glorysoft.xztpda.base.BaseActivity
import com.glorysoft.xztpda.databinding.ActivityTrackInGuideBinding
import com.huawei.hms.hmsscankit.ScanUtil
import com.huawei.hms.ml.scan.HmsScan
import kotlinx.android.synthetic.main.activity_track_in_guide.*
import kotlinx.android.synthetic.main.activity_track_out_da.*
import java.io.Serializable

/**
 * FileName: TrackInGuideActivity
 * Author: XJH
 * Date: 2022/4/12 16:13
 * Description:
 * History:
 */
class TrackInGuideActivity : BaseActivity<TrackInBaseViewModel, ActivityTrackInGuideBinding>(),
    View.OnClickListener {
    override fun layoutId(): Int {
        return R.layout.activity_track_in_guide
    }

    override fun initVariableId(): Int {
        return BR.trackInBaseViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle("进站")
        addActivity(this)

        ivLotScan.setOnClickListener(this)
        ivEqpScan.setOnClickListener(this)
        ivItemEqpScan.setOnClickListener(this)

        rvItemEqpList.adapter = viewModel.mAddCarrierAdapter
    }

    private fun jumpTrackin() {
        val bundle = Bundle()
        viewModel.lotInfoEntity?.eqpid = etEqpID.text.toString()
        bundle.putSerializable("LOTINFO", viewModel.lotInfoEntity)
        when (viewModel.lotInfoEntity?.trackinwizard) {
            //基本进站-需要载具&不需要物料
            "CarriersCherkTrackIn" -> {
                startActivity(TrackInCarrierActivity::class.java, bundle)
            }
            //基本进站-需要物料&需要载具
            "XztAssemblyTrackIn" -> {
                bundle.putString("TYPE", "XztAssemblyTrackIn")
                startActivity(TrackInCarrierMaterialActivity::class.java, bundle)
            }
            //基本进站-不需要物料&不需要载具
            "DefaultTrackIn" -> {
                startActivity(TrackInDefaultActivity::class.java, bundle)
            }
//            else -> {
//                viewModel.defUI.toastEvent.value =
//                    ToastMessageBean("找不到向导页名称:" + viewModel.lotInfoEntity?.trackinwizard, false)
//            }

        }
    }

    override fun initData() {

        initEdit(etLotID, object : EditTextListener {
            override fun onTextChangeListener(s: String) {
                viewModel.getLotInfo(s, etEqpID.text.toString())
            }

        })

        viewModel.defUI.callObserve.observe(this) {
            when (it.type) {
                0 -> {
                    mBinding.trackInBaseViewModel = viewModel
                    setTitle("进站" + "-" + viewModel.lotInfoEntity?.lot?.stepdesc)
                    if (viewModel.mStepList.contains(viewModel.lotInfoEntity?.lot?.stepname)) {
                        llEqpLayout.visibility = View.GONE
                    } else {
                        llEqpLayout.visibility = View.VISIBLE
                    }
                    if (viewModel.lotInfoEntity?.trackinwizard == "SubEqpTrackIn" ||
                        viewModel.lotInfoEntity?.trackinwizard == "XztSubCapaTrackIn"
                    ) {
                        setRightListener("下一步") {
                            if (viewModel.lotInfoEntity == null) {
                                viewModel.defUI.toastEvent.value =
                                    ToastMessageBean("未获取到批次信息", false)
                                return@setRightListener
                            }

                            if (llEqpLayout.visibility == View.VISIBLE) {
                                if (etEqpID.text.toString().isEmpty()) {
                                    viewModel.defUI.toastEvent.value =
                                        ToastMessageBean("请扫描设备ID", false)
                                    return@setRightListener
                                }
                            }

                            if (viewModel.mAddCarrierAdapter.data.isEmpty()) {
                                viewModel.defUI.toastEvent.value =
                                    ToastMessageBean("请扫描子设备到列表", false)
                                return@setRightListener
                            }
                            viewModel.lotInfoEntity?.eqpid = etEqpID.text.toString()
                            val bundle = Bundle()
                            bundle.putSerializable("LOTINFO", viewModel.lotInfoEntity)
                            bundle.putSerializable("ITEMEQP", viewModel.mAddCarrierAdapter.data as Serializable)
                            bundle.putString("TYPE", "XztSubCapaTrackIn")
                            startActivity(TrackInCarrierMaterialActivity::class.java, bundle)
                        }

                        llItemEqpLayout.visibility = View.VISIBLE
                    } else {
                        setRightListener("下一步") {
                            if (viewModel.lotInfoEntity == null) {
                                viewModel.defUI.toastEvent.value =
                                    ToastMessageBean("未获取到批次信息", false)
                                return@setRightListener
                            }

                            if (llEqpLayout.visibility == View.VISIBLE) {
                                if (etEqpID.text.toString().isEmpty()) {
                                    viewModel.defUI.toastEvent.value =
                                        ToastMessageBean("请扫描设备ID", false)
                                    return@setRightListener
                                }

                                viewModel.checkEqp(etEqpID.text.toString(), etLotID.text.toString())
                            } else {
                                jumpTrackin()
                            }

                        }

                        llItemEqpLayout.visibility = View.GONE
                    }
                }
                1 -> {
                    jumpTrackin()
                }
                2 -> {
                    etEqpID.setText("")
                }
            }


        }

        initEdit(etItemEqp, object : EditTextListener {
            override fun onTextChangeListener(s: String) {
                etItemEqp?.setText("")
                Handler().postDelayed({ etItemEqp?.requestFocus() }, 500)
                if (viewModel.mAddCarrierAdapter.data.contains(s)) {
                    viewModel.defUI.toastEvent.value = ToastMessageBean("当前子设备已添加到子设备列表中", false)
                    return
                }

                viewModel.mAddCarrierAdapter.addData(s)
            }

        })

        viewModel.defUI.startActivity.observe(this) { finish() }


    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.ivLotScan -> startScanForCode(this, 10001)
            R.id.ivEqpScan -> startScanForCode(this, 10002)
            R.id.ivItemEqpScan -> startScanForCode(this, 10003)
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {

        // 当扫码页面结束后，处理扫码结果。
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != RESULT_OK || data == null) {
            return
        }
        if (resultCode == RESULT_OK) {
            // 从onActivityResult返回data中，用ScanUtil.RESULT作为key值取到HmsScan返回值。
            when (val obj: Any = data.getParcelableExtra(ScanUtil.RESULT)) {
                is HmsScan -> {
                    val barCode = obj.getOriginalValue()
                    when (requestCode) {
                        10001 -> {
                            etLotID.setText(barCode)
                            viewModel.getLotInfo(barCode, etEqpID.text.toString())
                        }
                        10002 -> {
                            etEqpID.setText(barCode)
                        }
                        10003 -> {
                            if (viewModel.mAddCarrierAdapter.data.contains(barCode)) {
                                viewModel.defUI.toastEvent.value =
                                    ToastMessageBean("当前子设备已添加到子设备列表中", false)
                                return
                            }

                            viewModel.mAddCarrierAdapter.addData(barCode)
                        }
                    }
                }
            }


        }
    }
}